package com.demo.juc.volat;

/**
 * @ClassName ThreadVolatile
 * @Author bsy
 * @Description 可见性分析
 * @Date 2022/10/1 20:40
 * @Version 1.0
 */
public class ThreadVolatile {

    private int count = 0;

    public void countPlus() {
       count++;
    }

    /**
     * 多线程下的可见性
     * @return
     * @throws InterruptedException
     */
    private int getCount() throws InterruptedException {
        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 1000; i++) {
                countPlus();
            }
        });

        Thread t2 = new Thread(() -> {
            for (int i = 0; i < 1000; i++) {
                countPlus();
            }
        });

        t1.start();
        t2.start();
        t1.join();
        t2.join();
        return count;
    }

    public static void main(String[] args) throws InterruptedException {
        ThreadVolatile threadVolatile = new ThreadVolatile();
        int count = threadVolatile.getCount();
        System.out.println(count);
    }
}
